﻿using System.Web.Mvc;
using NC.Core.Models;
using NC.ViewModels;
using AccountUser = NC.Core.Models.AccountUser;
using NC.Core;

namespace NC.Controllers
{
    public class NepaliCornerController : Controller
    {
        private readonly IAccountUserService _accountUserService;

        public NepaliCornerController()
        {
            _accountUserService = new AccountUserService();
        }

        /// <summary>
        /// This is the starting page of the application that displays the nepalicorner homepage
        /// </summary>
        /// <returns>Homepage</returns>
        public ActionResult Index()
        { 
            return View("Index");
        }

        /// <summary>
        /// This method returns RegisterAccount view where the user can provide initial registration information
        /// </summary>
        /// <returns>Register account view</returns>
        public ActionResult RegisterAccount()
        {
            return View("RegisterAccount");
        }

        /// <summary>
        /// Post the account user to this method to register account
        /// </summary>
        /// <param name="accountUser">Account user that will be registered</param>
        [HttpPost]
        public ActionResult RegisterAccount(AccountUser accountUser)
        {  
            accountUser.FirstName = accountUser.FirstName ?? "";
            accountUser.MiddleName = accountUser.FirstName == null ? "" : accountUser.MiddleName;
            accountUser.LastName = accountUser.LastName ?? "";

            if (ModelState.IsValid)
            {
                if (_accountUserService.IsDuplicateEmail(accountUser.Email))
                {
                    ViewBag.Error = NcMessage.EmailAlreadyExists;
                }
                //To do: Check for valid email id
                //else if (is valid email)
                //{

                //}
                //To do: Check for valid password rule
                //else if (follows password rule)
                //{

                //}
                else
                {
                    accountUser.GenerateActivationCode();
                    var user = _accountUserService.RegisterAccountUser(accountUser);
                    var mailMessage = user.GetUserActivationMessage();
                    var email = new Email(mailMessage);
                    email.Send();
                    return RedirectToAction("Index");
                }
            }
            return View("Index");
        }

        /// <summary>
        /// This method activates user with the given activation code and redirects to GetLogInPage
        /// </summary>
        /// <param name="ac">Activation code for user who is going to be activated</param>
        /// <returns>Redirect to loginpage or error page</returns>
        public ActionResult ActivateUser(string ac)
        {
            if (_accountUserService.ActivateUser(ac))
            {
                return RedirectToAction("GetLogInPage"); 
            }
            return View("Error");
        }
        
        /// <summary>
        /// This returns LogIn page
        /// </summary>
        /// <returns></returns>
        public ActionResult GetLogInPage()
        {
            return View("GetLogInPage");
        }

        /// <summary>
        /// Validates the login information by calling service method to validate user
        /// </summary>
        /// <param name="users">User which is to be validated</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ValidateLogIn(UserLogin users)
        {
            if (ModelState.IsValid)
            {
                bool result = _accountUserService.ValidateUser(users.Email, users.Password);
                if (result)
                {
                    Session.Add("userEmail", users.Email);
                    ViewBag.email = users.Email;
                    return View("Index");
                }
            }
            return null;
        }
    }
}
